package view;

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.table.DefaultTableModel;

import services.DataCache;

class CheckListTM extends DefaultTableModel {	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4900415748354557271L;
	
	//private Vector dataRow;
	private Vector<String> columnNames;
	public CheckListTM ()
	{
		//dataRow = new Vector();
		columnNames = new Vector<String>();
		for (int i=0; i<DataCache.sColumnName.length; i++)
		{
			columnNames.add(DataCache.sColumnName[i]);
		}
		
		try {
			setColumnIdentifiers(columnNames);
			//setDataVector(dataRow, columnNames);
		}
		catch (ArrayIndexOutOfBoundsException e){			
			
		}
	}
	
	private Vector dataValidate(Vector rowData)
	{
		//"Country" 0, "Course" 1, "POD" 2, "Student" 3, "Start Date" 4, "Days" 5, "Image" 6, "Instructor" 7
		int i;
		Pattern p;					
		Matcher m;
		boolean isESX = false;
		boolean isUNIX = false;
		for (i=0; i<rowData.size(); i++)
		{
			String tmpStr = rowData.elementAt(i).toString();
			switch (i)
				{
				//Region
				case 0:
					rowData.setElementAt(tmpStr, i);
				break;
				//Course
				case 1:
					p = Pattern.compile("BE");
					m = p.matcher(tmpStr);
					if (m.find())
					rowData.setElementAt(m.replaceAll("Backup Exec "),i);
					p = Pattern.compile("NBU");
					m = p.matcher(tmpStr);
					if (m.find())
					rowData.setElementAt(m.replaceAll("NetBackup "),i);
					p = Pattern.compile("SF");
					m = p.matcher(tmpStr);
					if (m.find())
					rowData.setElementAt(m.replaceAll("Storage Foundation "),i);
					p = Pattern.compile("EV");
					m = p.matcher(tmpStr);
					if (m.find())
					rowData.setElementAt(m.replaceAll("Enterprise Vault "), i);
					p = Pattern.compile("VCS");
					m = p.matcher(tmpStr);
					if (m.find())
					rowData.setElementAt(m.replaceAll("Veritas Cluster Server "),i);
					p = Pattern.compile("SEP");
					m = p.matcher(tmpStr);
					if (m.find())
					rowData.setElementAt(m.replaceAll("Symantec Endpoint Protection "),i);
					p = Pattern.compile("CCS");
					m = p.matcher(tmpStr);
					if (m.find())
					rowData.setElementAt(m.replaceAll("Control Compliance Suite "),i);
					p = Pattern.compile("DLP");
					m = p.matcher(tmpStr);
					if (m.find())
					rowData.setElementAt(m.replaceAll("Data Loss Prevention "),i);
					break;
				//POD
				case 2:
					p = Pattern.compile("^([0-9]+).*");
					m = p.matcher(tmpStr.trim());
					if (m.matches())
					{
						rowData.setElementAt("Pod".toUpperCase().concat(m.group(1)),i);
					}
					else
					{
						rowData.setElementAt(tmpStr.toUpperCase(), i);
					}
					
					if (tmpStr.toLowerCase().contains("esx")){isESX = true;}
					else {isESX = false;}
					break;
				//Student
				case 3:
					// if a double string
					p = Pattern.compile("([0-9]+)\\..*");
					m = p.matcher(tmpStr);
					while (m.find()){
						rowData.setElementAt(m.group(1), i);
					}
					// if a none digital string
					p = Pattern.compile("[^0-9].*");
					m = p.matcher(tmpStr);
					if (m.find()){
						rowData.setElementAt(m.replaceAll(""), i);
						if (rowData.elementAt(i).toString().isEmpty()){
							rowData.setElementAt("1",i);
						}
					}
					if (tmpStr.isEmpty()){
						rowData.setElementAt("1",i);
					}
					break;
				//Start Date
				case 4:/*
					if (tmpStr.isEmpty()){
						rowData.setElementAt(DataCache.dToday,i);
					}*/
					break;
				//Days
				case 5:
					p = Pattern.compile("([0-9]+)\\..*");
					m = p.matcher(tmpStr);
					while (m.find()){
						rowData.setElementAt(m.group(1), i);
					}
					if (tmpStr.isEmpty()){
						rowData.setElementAt("0",i);
					}
					break;
				case 6:
					if (tmpStr.isEmpty() && isESX) {
						rowData.setElementAt("ESX", i);
					}
					if (tmpStr.toLowerCase().contains("select") && isESX){
						rowData.setElementAt("ESX", i);
					}
					break;
				case 7:
					break;
				default:
					break;
				}
		}
		return rowData;
	}
	
	public void addRow_s(Vector rowData)
	{
		addRow(dataValidate(rowData));
	}
	
	public Vector getRowAt(int lineNumber)
	{
		Vector tmpVector = new Vector();
		tmpVector = (Vector) getDataVector().elementAt(lineNumber);
		return tmpVector;
	}
	
	public void updateRow_s(Vector newRowData, int Index)
	{
		Vector<Vector> allData = getDataVector();
		Vector<String> vecColNames = new Vector<String>();
		for (int i=0; i<DataCache.sColumnName.length; i++)
		{
			vecColNames.add(DataCache.sColumnName[i]);
		}
		allData.setElementAt(newRowData, Index);
		setDataVector(allData, vecColNames);
	}
	
	@Override
	public boolean isCellEditable(int row, int column) {
        return true;
    }
	
	public Vector<String> getColumnNames()
	{
		return columnNames;
	}
}

